//
//  main.swift
//  Night
//
//  Created by ZTT on 2017/9/14.
//  Copyright © 2017年 ZTT(支婷婷）. All rights reserved.
//

import Foundation

struct Student{
    var name:String
    var math:Int
    var english:Int
    var avg:Float
    
    init (name:String,math:Int,english:Int)
    {
        self.name=name;
        self.math=math;
        self.english=english;
        self.avg=Float(self.math+self.english)/2
    }
}


func sortWithAverage(students:[Student]) -> [Student] {
    var result = students
    for i in 0...9{
        for j in i...9
        {
            if result[i].avg>result[j].avg
            {
                let tmp = result[i]
                result[i] = result[j]
                result[j] = tmp
            }
        }
    }


return result
}


var student01 = Student(name: "st01", math: 67, english: 98)
var student02 = Student(name: "st02", math: 87, english: 56)
var student03 = Student(name: "st03", math: 47, english: 86)
var student04 = Student(name: "st04", math: 65, english: 88)
var student05 = Student(name: "st05", math: 85, english: 97)
var student06 = Student(name: "st06", math: 85, english: 94)
var student07 = Student(name: "st07", math: 92, english: 76)
var student08 = Student(name: "st08", math: 93, english: 66)
var student09 = Student(name: "st09", math: 63, english: 93)
var student10 = Student(name: "st10", math: 99, english: 82)


var result = sortWithAverage(students: [student01,
                                        student02,
                                        student03,
                                        student04,
                                        student05,
                                        student06,
                                        student07,
                                        student08,
                                        student09,
                                        student10])

print("英语和数学平均分的成绩从小到大排列为:")

for i in 0..<result.count{
    print("姓名:\(result[i].name),数学成绩:\(result[i].math),英语成绩:\(result[i].english),平均分:\(result[i].avg)")
}

